package com.xique.park.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.park.api.request.RemoteBlackCarRequest;
import com.xique.park.api.request.RemoteGetCarListRequest;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.User;
import com.xique.park.bean.request.app.ParkCarAddRequest;
import com.xique.park.bean.request.app.ParkCarDeleteRequest;
import com.xique.park.bean.request.app.ParkCarEditRequest;
import com.xique.park.bean.request.web.parkcar.BlackCarRequest;
import com.xique.park.bean.request.web.parkcar.ParkCarRequest;
import com.xique.park.service.biz.service.IParkCarApiService;
import com.xique.park.service.biz.service.IParkCarBizService;
import com.xique.park.service.service.IParkCarService;
import com.xique.park.service.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 对外车辆接口
 *
 * @author caogq
 */
@Slf4j
@RestController
@RequestMapping("/api/car")
public class ApiParkCarController extends BaseController {

    @Autowired
    private IParkCarApiService parkCarApiService;

    @Autowired
    private IParkCarBizService parkCarBizService;

    @Autowired
    private IParkCarService parkCarService;

    @Autowired
    private IUserService userService;

    /**
     * 新增车辆
     */
    @PostMapping("/addCar")
    public AjaxResult addCar(@Validated @RequestBody ParkCarAddRequest request) {
        log.info("[车辆接口]-新增车辆，请求参数：{}", request);

        if (request.getParkId() == null){
            log.info("[车辆接口]-新增车辆，车场ID为Null");
            return AjaxResult.error("车场ID为Null");
        }

        if (request.getUserId() != null){
            User user = new User();
            user.setParkId(request.getParkId());
            user.setId(request.getUserId());
            List<User> userList = userService.selectUserList(user);
            if (userList.isEmpty()){
                return AjaxResult.error("用户【" + request.getUserId() + "】,不属于车场【" + request.getParkId() + "】");
            }
        }

        AjaxResult result = parkCarBizService.addWebParkCar(request);
        log.info("[车辆接口]-新增车辆，新增成功");

        return result;
    }


    /**
     * 修改车辆
     */
    @PutMapping("/updateCar")
    public AjaxResult updateCar(@Validated @RequestBody ParkCarEditRequest request) {
        log.info("[车辆接口]-修改车辆，请求参数：{}", request);
        AjaxResult result = parkCarBizService.editWebParkCar(request);
        log.info("[车辆接口]-修改车辆，新增成功");
        return result;
    }

    /**
     * 删除车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult removeCar(@RequestParam("carId") Long carId, @RequestParam("parkId") Long parkId) {
        log.info("[车辆接口]-删除车辆，请求参数parkId：{}，请求参数carId：{}", parkId, carId);
        AjaxResult result = parkCarApiService.removeCar(carId, parkId);
        log.info("[车辆接口]-删除车辆，删除成功");
        return result;
    }

    /**
     * 查询车辆列表
     */
    @PostMapping("/list")
    public AjaxResult parkCarList(@Valid @RequestBody RemoteGetCarListRequest remoteRequest) {
        log.info("[车辆接口]-车辆列表，请求参数：{}", remoteRequest);
        ParkCarRequest request = OrikaUtil.convert(remoteRequest, ParkCarRequest.class);
        AjaxResult result = parkCarApiService.getCarList(request);
        log.info("[车辆接口]-车辆列表，结果：{}", result);
        return result;
    }

    /**
     * 查询车辆管理类型
     */
    @GetMapping("/mngType/list")
    public AjaxResult mngTypeList(@RequestParam("parkId") Long parkId) {
        log.info("[车辆接口]-车辆管理类型列表，请求参数：parkId:{}", parkId);
        AjaxResult result = parkCarApiService.getMngTypeList(parkId);
        log.info("[车辆接口]-车辆管理类型列表，结果：{}", result);
        return result;
    }

    /**
     * 车辆拉黑
     */
    @PostMapping("/addBlack")
    public AjaxResult addBlack(@Valid @RequestBody RemoteBlackCarRequest remoteRequest) {
        log.info("[车辆接口]-添加黑名单，请求参数：{}", remoteRequest);
        BlackCarRequest request = OrikaUtil.convert(remoteRequest, BlackCarRequest.class);
        request.setUserId(0L);
        AjaxResult result = parkCarApiService.addBlack(request);
        log.info("[车辆接口]-添加黑名单，结果：{}", result);
        return result;
    }

    @DeleteMapping("/removeBlack")
    public AjaxResult removeBlack(@RequestParam("carId") Long carId, @RequestParam("parkId") Long parkId) {
        log.info("[车辆接口]-移出黑名单，请求参数：carId:{}", carId);
        ParkCar search = new ParkCar();
        search.setParkId(parkId);
        search.setId(carId);
        List<ParkCar> list = parkCarService.selectParkCarList(search);
        if (CollectionUtil.isEmpty(list)) {
            return AjaxResult.error("该车辆不属于当前车场");
        }
        ParkCarDeleteRequest request = new ParkCarDeleteRequest();
        request.setCarId(carId);
        request.setParkId(parkId);
        request.setUserId(0L);
        AjaxResult result = parkCarBizService.removeBlack(request);
        log.info("[车辆接口]-移出黑名单，结果：{}", result);
        return result;
    }
}
